//
//  CQTabBarButton.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/23.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQTabBarButton.h"
#import "CQBadgeBtn.h"

#define CQProportion 0.7

@interface CQTabBarButton()

@property (nonatomic, weak) CQBadgeBtn *badgeBtn;

@end

@implementation CQTabBarButton
#pragma mark - 不要系统的高亮状态
-(void)setHighlighted:(BOOL)highlighted
{}


#pragma mark - 懒加载
- (CQBadgeBtn *)badgeBtn
{
    if (_badgeBtn == nil) {
       CQBadgeBtn *btn   = [[CQBadgeBtn alloc]init];
        _badgeBtn = btn;
        //badgeBtn 添加到 按钮上
        [self addSubview:btn];
        
    }
    return _badgeBtn;
}


#pragma mark - init调用

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        
        //文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        //图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        
        //设置文字大小
        self.titleLabel.font  = [UIFont systemFontOfSize:13];
        
        
    }
    return  self;
}


#pragma mark - set方法 给按钮赋值
-(void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
    
    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionNew context:nil];
    
    
    

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    //设置正常的图片
    [self setImage:self.item.image forState:UIControlStateNormal];
    //设置选中的图片
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    //设置正常的文字
    [self setTitle:self.item.title forState:UIControlStateNormal];
    //设置badgeValue的值
    self.badgeBtn.badgeValue = self.item.badgeValue;

}
#pragma mark - 布局图和文字的位子 
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat imgX = 0;
    CGFloat imgY = 0;
    CGFloat imgW = self.bounds.size.width;
    CGFloat imgH = self.bounds.size.height*CQProportion;
    self.imageView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    
    CGFloat titleX = 0;
    CGFloat titleY = imgH - 3;
    CGFloat titleW = self.bounds.size.width;
    CGFloat titleH = self.bounds.size.height - titleY;
    self.titleLabel.frame  = CGRectMake(titleX, titleY, titleW, titleH);
    CGRect frame = self.badgeBtn.frame;
    frame.origin.x = self.frame.size.width - self.badgeBtn.frame.size.width - 10;
    frame.origin.y = 0;
    //再把值刷新
    self.badgeBtn.frame = frame;
    
}


@end
